/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccYaopinlanyong;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccYaopinlanyongRepository;
import me.zhengjie.modules.system.service.DccYaopinlanyongService;
import me.zhengjie.modules.system.service.dto.DccYaopinlanyongDto;
import me.zhengjie.modules.system.service.dto.DccYaopinlanyongQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccYaopinlanyongMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-05-17
**/
@Service
@RequiredArgsConstructor
public class DccYaopinlanyongServiceImpl implements DccYaopinlanyongService {

    private final DccYaopinlanyongRepository dccYaopinlanyongRepository;
    private final DccYaopinlanyongMapper dccYaopinlanyongMapper;

    @Override
    public Map<String,Object> queryAll(DccYaopinlanyongQueryCriteria criteria, Pageable pageable){
        Page<DccYaopinlanyong> page = dccYaopinlanyongRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccYaopinlanyongMapper::toDto));
    }

    @Override
    public List<DccYaopinlanyongDto> queryAll(DccYaopinlanyongQueryCriteria criteria){
        return dccYaopinlanyongMapper.toDto(dccYaopinlanyongRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccYaopinlanyongDto findById(Integer id) {
        DccYaopinlanyong dccYaopinlanyong = dccYaopinlanyongRepository.findById(id).orElseGet(DccYaopinlanyong::new);
        ValidationUtil.isNull(dccYaopinlanyong.getId(),"DccYaopinlanyong","id",id);
        return dccYaopinlanyongMapper.toDto(dccYaopinlanyong);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccYaopinlanyongDto create(DccYaopinlanyong resources) {
        return dccYaopinlanyongMapper.toDto(dccYaopinlanyongRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccYaopinlanyong resources) {
        DccYaopinlanyong dccYaopinlanyong = dccYaopinlanyongRepository.findById(resources.getId()).orElseGet(DccYaopinlanyong::new);
        ValidationUtil.isNull( dccYaopinlanyong.getId(),"DccYaopinlanyong","id",resources.getId());
        dccYaopinlanyong.copy(resources);
        dccYaopinlanyongRepository.save(dccYaopinlanyong);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccYaopinlanyongRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccYaopinlanyongDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccYaopinlanyongDto dccYaopinlanyong : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("状态0不展示 1展示", dccYaopinlanyong.getStatus());
            map.put("名称", dccYaopinlanyong.getName());
            map.put("类别", dccYaopinlanyong.getType());
            map.put("成分", dccYaopinlanyong.getChengfen());
            map.put("简介", dccYaopinlanyong.getJainjie());
            map.put("成瘾性", dccYaopinlanyong.getChenyinxin());
            map.put("适应症", dccYaopinlanyong.getShiyinzhen());
            map.put("操作者", dccYaopinlanyong.getOperator());
            map.put(" createdAt",  dccYaopinlanyong.getCreatedAt());
            map.put(" updatedAt",  dccYaopinlanyong.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}